package com.glsc.ngateway.platform.repo.costallocation;

import com.glsc.ngateway.platform.domain.costallocation.BusiSystemInfo;
import com.glsc.ngateway.platform.domain.costallocation.SystemComponentCategory;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;

@Repository
public interface BusiSystemInfoRepo extends JpaRepository<BusiSystemInfo, Long>, JpaSpecificationExecutor<BusiSystemInfo> {

    @Query(value = "select t from BusiSystemInfo t where t.id = :id")
    BusiSystemInfo queryById(long id);

    @Transactional
    @Modifying(clearAutomatically = true,flushAutomatically = true)
    @Query(value = "update t_busisystem_info u set u.vc_system_user_id = :userId ,u.vc_system_class = :systemClass," +
            "u.vc_main_class = :mainClass,u.vc_operator =:operator ,u.dt_update_time=:updatetime where u.l_id = :id",nativeQuery = true)
    void updateSystemUserId(@Param("id") long id, @Param("userId") String userId,
                            @Param("operator") String operator,@Param("updatetime") LocalDateTime updatetime,
                            @Param("mainClass") String mainClass,@Param("systemClass") String systemClass);


    @Transactional
    @Modifying(clearAutomatically = true,flushAutomatically = true)
    @Query(value = "update t_busisystem_info u set u.vc_system_user_id = :userId ,u.vc_system_class = :systemClass," +
            "u.vc_main_class = :mainClass,u.vc_special_dep = :depJsonString,u.vc_operator =:operator ,u.dt_update_time=:updatetime where u.l_id = :id",nativeQuery = true)
    void updateSystemUserIdAllDep(@Param("id") long id, @Param("userId") String userId,
                            @Param("operator") String operator,@Param("updatetime") LocalDateTime updatetime,
                            @Param("mainClass") String mainClass,@Param("systemClass") String systemClass,
                            @Param("depJsonString") String depJsonString);



    @Query(value = "select u  from BusiSystemInfo u where u.systemClassNo in :systemClassNoSet")
    List<BusiSystemInfo> queryBySystemClassNo(@Param("systemClassNoSet") Set<String> systemClassNoSet);


    @Query(value = "select u  from BusiSystemInfo u where u.systemClassNo = :systemClassNo")
    BusiSystemInfo queryBySystemClassNo(@Param("systemClassNo") String systemClassNo);

    @Query(value = "select u  from BusiSystemInfo u where u.specilaDep is not null or u.specilaDep !=''")
    List<BusiSystemInfo> queryBySpecilaDepNotNull();

}
